﻿Imports Publics
Imports DevExpress.XtraGrid.Columns
Imports System.Windows.Forms
Imports Vla.Vms

Public Class FrmTaiKhoan_NguoiSuDung
    Private lstIDDV As New List(Of String)

    Private _ok As Boolean
    Public Property ok() As Boolean
        Get
            Return _ok
        End Get
        Set(ByVal value As Boolean)
            _ok = value
        End Set
    End Property

    Private _lstID_NguoiDungChon As New List(Of String)
    Public Property lstID_NguoiDungChon() As List(Of String)
        Get
            Return _lstID_NguoiDungChon
        End Get
        Set(ByVal value As List(Of String))
            _lstID_NguoiDungChon = value
        End Set
    End Property

    Private _objND As DM_NhanVien
    Public Property objND() As DM_NhanVien
        Get
            Return _objND
        End Get
        Set(ByVal value As DM_NhanVien)
            _objND = value
        End Set
    End Property

    Private _lstNhanVien As New List(Of DM_NhanVien)
    Public Property lstNhanVien() As List(Of DM_NhanVien)
        Get
            Return _lstNhanVien
        End Get
        Set(ByVal value As List(Of DM_NhanVien))
            _lstNhanVien = value
        End Set
    End Property

    Private _laChon As Boolean
    Public Property laChon() As Boolean
        Get
            Return _laChon
        End Get
        Set(ByVal value As Boolean)
            _laChon = value
        End Set
    End Property

    'Private Sub FillDataToTreeView()
    '    treDonVi.DataSource = From ds In dbTV.DM_DonVis Where ds.InUsed _
    '                          Order By ds.ThuTu, ds.TenDonVi _
    '                          Select ds.ID, ds.ID_Childs, ds.TenDonVi, ds.ParentID
    '    treDonVi.Refresh()
    'End Sub
    Private SuperAdmin As String = "SuperAdmin"

    Private Sub FillDataToGridView()
        'If vUser.CanBo.UserName = SuperAdmin Then
        grdNguoiDung.DataSource = (From ds In dbTV.DM_NhanViens _
                               Where ds.InUsed AndAlso lstIDDV.Contains(ds.ID_DonVi) _
                               Order By ds.MaSo Ascending Select New With {.ID = ds.ID, .ID_DonVi = ds.ID_DonVi, _
                              .MaSo = ds.MaSo, .HoTen = ds.HoTen, .UserName = ds.UserName, .DienThoai = ds.DienThoai, _
                              .HanSuDung = ds.HanSuDung, ds.DM_DonVi.TenDonVi, _
                              .Chon = IIf(lstID_NguoiDungChon.Contains(ds.ID), True, False)}).ToList

        grdNguoiDung.RefreshDataSource()
        grvNguoiDung.RefreshData()
        'Else
        'grdNguoiDung.DataSource = (From ds In dbTV.DM_NhanViens _
        '                       Where ds.InUsed AndAlso lstIDDV.Contains(ds.ID_DonVi) _
        '                       And ds.UserName <> SuperAdmin _
        '                       Order By ds.MaSo Ascending Select New With {.ID = ds.ID, .ID_DonVi = ds.ID_DonVi, _
        '                      .MaSo = ds.MaSo, .HoTen = ds.HoTen, .UserName = ds.UserName, .DienThoai = ds.DienThoai, _
        '                      .HanSuDung = ds.HanSuDung, ds.DM_DonVi.TenDonVi, _
        '                      .Chon = IIf(lstID_NguoiDungChon.Contains(ds.ID), True, False)}).ToList

        'grdNguoiDung.RefreshDataSource()
        'grvNguoiDung.RefreshData()
        ' End If

    End Sub

    Private Sub FrmTaiKhoan_NguoiSuDung_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
        If laChon AndAlso e.KeyCode = Keys.Enter Then
            btnChon_ItemClick(Nothing, Nothing)
        End If
        'Thêm
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, BtnThemMoi) Then
            BtnThemTK_ItemClick(Nothing, Nothing)
        End If
        'Sửa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, BtnSuaDoi) Then
            BtnSuaDoi_ItemClick(Nothing, Nothing)
        End If
        'Xóa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, BtnXoa) Then
            BtnXoa_ItemClick(Nothing, Nothing)
        End If
        'Làm tươi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH, BtnLamTuoi) Then
            BtnLamTuoi_ItemClick(Nothing, Nothing)
        End If
        'Xuất DL
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXPORT, BtnXuatDuLieu) Then
            BtnXuatDuLieu_ItemClick(Nothing, Nothing)
        End If
        'Trợ giúp
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pINFO, btnTroGiup) Then
            btnTroGiup_ItemClick(Nothing, Nothing)
        End If
        'Thoát			
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXIT, btnDong) Then
            btnDong_ItemClick(Nothing, Nothing)
        End If
    End Sub
    Public Shared Them, Sua, Xoa, PPhanQuyen As Boolean
    Private Sub PhanQuyen()
        BtnThemTK.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        BtnCopy.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        BtnSuaDoi.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        BtnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnPhanQuyen.Visibility = IIf(PPhanQuyen, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub
    Private Sub FrmTaiKhoan_NguoiSuDung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            lstIDDV = (From ds In dbTV.DM_DonVis Where ds.InUsed Select ds.ID).ToList
            'FillDataToTreeView()
            FillDataToGridView()
            If laChon Then
                btnChon.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
                grdcolChon.Visible = True
                grvNguoiDung.Focus()
                grvNguoiDung.FocusedRowHandle = 0
            Else
                grdcolChon.Visible = False
                btnChon.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
            End If
            'treDonVi.CollapseAll()
            'treDonVi.FocusedNode = treDonVi.Nodes.Item(0)

            PhanQuyen()
        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub btnChon_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnChon.ItemClick
        Try
            lstNhanVien.Clear()
            grvNguoiDung.FocusedRowHandle = DevExpress.XtraGrid.GridControl.AutoFilterRowHandle
            For i = 0 To grvNguoiDung.RowCount - 1
                If CBool(grvNguoiDung.GetRowCellValue(i, grdcolChon)) = True Then
                    Dim id = grvNguoiDung.GetRowCellValue(i, grdcolID)
                    Dim obj As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed AndAlso ds.ID = CStr(id) Select ds).FirstOrDefault

                    If obj IsNot Nothing Then
                        lstNhanVien.Add(obj)
                    End If
                End If
            Next

            If lstNhanVien.Count > 0 Then
                ok = True
                Me.Close()
            End If
        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub BtnThemTK_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnThemTK.ItemClick
        Dim frm As New FrmCapNhatTaiKhoan_NguoiSuDung
        frm.ActionForm = vAddAction
        frm.ShowDialog()
        If Not String.IsNullOrEmpty(frm.ID) Then
            'FillDataToTreeView()
            FillDataToGridView()
            For i = 0 To grvNguoiDung.RowCount - 1
                If grvNguoiDung.GetRowCellValue(i, grdcolID).Equals(frm.ID) Then
                    grvNguoiDung.FocusedRowHandle = i
                    Exit For
                End If
            Next
        End If
    End Sub

    Private Sub BtnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnSuaDoi.ItemClick
        Dim index As Integer = grvNguoiDung.FocusedRowHandle
        Dim objND As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
                                    And ds.ID = CStr(grvNguoiDung.GetRowCellValue(index, grdcolID)) Select ds).FirstOrDefault

        If objND IsNot Nothing Then
            Dim frm As New FrmCapNhatTaiKhoan_NguoiSuDung
            frm.ActionForm = vEditAction
            frm.objNguoiDung = objND
            frm.ShowDialog()
            FillDataToGridView()
            grvNguoiDung.FocusedRowHandle = index
        End If
    End Sub

    Private Sub BtnCopy_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnCopy.ItemClick
        Dim index As Integer = grvNguoiDung.FocusedRowHandle
        Dim objND As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
                                    And ds.ID = CStr(grvNguoiDung.GetRowCellValue(index, grdcolID)) Select ds).FirstOrDefault

        If objND IsNot Nothing Then
            Dim frm As New FrmCapNhatTaiKhoan_NguoiSuDung
            frm.ActionForm = vCopyAction
            frm.objNguoiDung = objND
            frm.ShowDialog()
            If Not String.IsNullOrEmpty(frm.ID) Then
                'FillDataToTreeView()
                FillDataToGridView()
                For i = 0 To grvNguoiDung.RowCount - 1
                    If grvNguoiDung.GetRowCellValue(i, grdcolID).Equals(frm.ID) Then
                        grvNguoiDung.FocusedRowHandle = i
                        Exit For
                    End If
                Next
            End If
        End If
    End Sub

    Private Sub BtnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnXoa.ItemClick
        Dim id = grvNguoiDung.GetRowCellValue(grvNguoiDung.FocusedRowHandle, grdcolID)
        If id IsNot Nothing AndAlso Not String.IsNullOrEmpty(id.ToString.Trim) Then
            Dim objND As DM_NhanVien = (From ds In dbTV.DM_NhanViens Where ds.InUsed _
                                        And ds.ID = CStr(grvNguoiDung.GetRowCellValue(grvNguoiDung.FocusedRowHandle, grdcolID)) Select ds).FirstOrDefault

            If objND IsNot Nothing Then
                If vUser.CanBo.UserName.ToLower <> "superadmin" Then
                    If objND.ID = vUser.CanBo.ID Then
                        MsgErr("Bạn không thể xóa tài khoản đang đăng nhập!")
                        Return
                    ElseIf objND.UserName.ToLower = "admin" Then
                        MsgErr("Bạn không thể xóa tài khoản quản trị hệ thống!")
                        Return
                    End If
                End If
                If MsgAskDelete("Bạn có muốn xóa bản ghi này không?") = Windows.Forms.DialogResult.Yes Then
                    'Xóa người dùng kiểm tra xóa bảng quyền người dùng ( 
                    Dim objQuyenND = (From ds In dbTV.TG_NguoiDung_Quyens _
                                      Where ds.InUsed = True AndAlso ds.ID_NguoiDung = objND.ID _
                                      Select ds).ToList
                    For Each item In objQuyenND
                        item.InUsed = False
                        item.EditedOn = dbTV.GetDateTimeServer.Value
                    Next
                    '
                    objND.InUsed = False
                    objND.EditedOn = CDate(dbTV.GetDateTimeServer)
                    iSubmitChange()
                    'Ghi log hệ thống
                    SaveLog("Xóa tài khoản người dùng", "Xóa", "Mã = " & objND.MaSo & "; Tên = " & objND.HoTen, "")
                    'FillDataToTreeView()
                    FillDataToGridView()
                End If
            End If
        End If
    End Sub

    Private Sub BtnLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnLamTuoi.ItemClick
        ConnectToDB()
        'FillDataToTreeView()
        FillDataToGridView()
    End Sub

    Private Sub BtnXuatDuLieu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnXuatDuLieu.ItemClick
        Export_Dialog(SaveFileDialog1, grdNguoiDung, Nothing)
    End Sub

    Private Sub btnPhanQuyen_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnPhanQuyen.ItemClick
        Me.UseWaitCursor = True
        Me.Cursor = Cursors.WaitCursor
        Dim id = grvNguoiDung.GetRowCellValue(grvNguoiDung.FocusedRowHandle, grdcolID)
        If id IsNot Nothing Then
            Dim frm As New FrmCapQuyenNguoiDung
            frm.NguoiDung = (From ds In dbTV.DM_NhanViens Where ds.InUsed And ds.ID = CStr(id) Select ds).FirstOrDefault
            frm.ShowDialog()
            Me.UseWaitCursor = False
            Me.Cursor = Cursors.Default
        End If
    End Sub

    Private Sub btnTroGiup_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnTroGiup.ItemClick

    End Sub

    Private Sub btnDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDong.ItemClick
        Me.Close()
    End Sub

     

    Private Sub btnCapLaiMatKhau_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCapLaiMatKhau.ItemClick
        Dim id = grvNguoiDung.GetRowCellValue(grvNguoiDung.FocusedRowHandle, grdcolID)
        If id IsNot Nothing AndAlso id.ToString.Trim <> "" Then
            Dim obj = (From ds In dbTV.DM_NhanViens Where ds.InUsed AndAlso ds.ID = CStr(id) Select ds).FirstOrDefault
            If obj IsNot Nothing Then
                'Neu la superadmin thi cho reset het
                If vUser.CanBo.UserName.ToLower = "superadmin" Then
                    ResetMatKhau(obj)
                    Return
                End If
                'Neu khong la superadmin thi chan khong cho sua superadmin
                If obj.UserName.ToLower = "superadmin" Then
                    MsgErr("Không được phép cấp lại mật khẩu cho SuperAdmin !!!")
                    Return
                End If
                'Neu la admin thi cho reset user cua don vi
                If vUser.CanBo.UserName.ToLower = "admin" Then
                    ResetMatKhau(obj)
                    Return
                End If
                'Neu khong la admin thi chan khong cho sua admin
                If obj.UserName.ToLower = "admin" Then
                    MsgErr("Không được phép cấp lại mật khẩu cho Admin !!!")
                Else
                    ResetMatKhau(obj)
                End If
            End If
        End If
    End Sub

    Private Sub ResetMatKhau(ByVal objNV As DM_NhanVien)
        If objNV Is Nothing Then Return
        If MsgAskDelete(String.Format("Bạn muốn cấp lại mật khẩu cho tài khoản ""{0}""." & vbCrLf & "Khi cấp lại tài khoản mật khẩu sẽ trở về là 1." & vbCrLf & "Xin vui lòng kiểm tra lại mật khẩu của tài khoản ""{0}"" vừa đổi !", objNV.UserName)) = Windows.Forms.DialogResult.Yes Then
            objNV.EditedOn = dbTV.GetDateTimeServer.Value
            objNV.PassWord = Vla.Vms.ClsUtil.MD5Encrypt("1")
            iSubmitChange()
            ''
            SaveLog("Cấp lại mật khẩu tài khoản: " & objNV.UserName, "Cấp lại mật khẩu", "Mã = " & objNV.MaSo & "; Tên = " & objNV.HoTen, "")
            MsgErr("Cấp lại mật khẩu thành công !!!")
        End If
    End Sub

End Class